package org.tulkas.data;

import java.net.UnknownHostException;
import java.util.Properties;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;

/**
 * @author <a href="mailto:tulkas.web@gmail.com">Tulkas</a>
 *
 */
public class MongoConnector {
	private Mongo mongo = null;
	private DB db = null;

	public MongoConnector(Properties props) throws NumberFormatException,
			MongoException, UnknownHostException {
		String host = props.getProperty("connection.host");
		String port = props.getProperty("connection.port");
		String dbName = props.getProperty("connection.dbName");
		String user = props.getProperty("connection.username");
		String pass = props.getProperty("connection.password");

		mongo = new Mongo(new ServerAddress(host, Integer.parseInt(port)),
				createMongoOptions(props));
		DB db = mongo.getDB(dbName);
		if (user != null && !"".equals(user)) {
			db.authenticate(user, (pass == null) ? null : pass.toCharArray());
		}
	}

	private MongoOptions createMongoOptions(Properties props) {
		MongoOptions options = new MongoOptions();
		if (props.containsKey("options.autoConnectRetry")) {
			options.autoConnectRetry = Boolean.parseBoolean(props
					.getProperty("options.autoConnectRetry"));
		}
		if (props.containsKey("options.connectionsPerHost")) {
			options.connectionsPerHost = Integer.parseInt(props
					.getProperty("options.connectionsPerHost"));
		}
		if (props.containsKey("options.connectTimeout")) {
			options.connectTimeout = Integer.parseInt(props
					.getProperty("options.connectTimeout"));
		}
		if (props.containsKey("options.description")) {
			options.description = props.getProperty("options.description");
		}
		if (props.containsKey("options.fsync")) {
			options.fsync = Boolean.parseBoolean(props
					.getProperty("options.fsync"));
		}
		if (props.containsKey("options.j")) {
			options.j = Boolean.parseBoolean(props.getProperty("options.j"));
		}
		if (props.containsKey("options.maxAutoConnectRetryTime")) {
			options.maxAutoConnectRetryTime = Long.parseLong(props
					.getProperty("options.maxAutoConnectRetryTime"));
		}
		if (props.containsKey("options.maxWaitTime")) {
			options.maxWaitTime = Integer.parseInt(props
					.getProperty("options.maxWaitTime"));
		}
		if (props.containsKey("options.safe")) {
			options.safe = Boolean.parseBoolean(props
					.getProperty("options.safe"));
		}
		if (props.containsKey("options.socketKeepAlive")) {
			options.socketKeepAlive = Boolean.parseBoolean(props
					.getProperty("options.socketKeepAlive"));
		}
		if (props.containsKey("options.socketTimeout")) {
			options.socketTimeout = Integer.parseInt(props
					.getProperty("options.socketTimeout"));
		}
		if (props
				.containsKey("options.threadsAllowedToBlockForConnectionMultiplier")) {
			options.threadsAllowedToBlockForConnectionMultiplier = Integer
					.parseInt(props
							.getProperty("options.threadsAllowedToBlockForConnectionMultiplier"));
		}
		if (props.containsKey("options.socketTimeout")) {
			options.w = Integer.parseInt(props.getProperty("options.w"));
		}
		if (props.containsKey("options.wtimeout")) {
			options.wtimeout = Integer.parseInt(props
					.getProperty("options.wtimeout"));
		}
		return options;
	}

	public DB getDB() {
		return db;
	}

	public Mongo getMongo() {
		return mongo;
	}
}
